/**
 *  @file SDLAudioManager.cpp
 *
 *  @date 3-dec-2009
 *  @author Ives van der Flaas
 */

#include <sstream>
#include <cassert>
#include <SDL.h>
#include <SDL_mixer.h>
#include <tr1/memory>
#include "configManager.h"
#include "SDLAudioManager.h"
#include "SDLException.h"
#include "SDLMusicDeleter.h"
#include "SDLResourceManager.h"
#include "SDLChunkDeleter.h"



namespace si_sdl
{


SDLAudioManager::SDLAudioManager()
{
	int audio_rate = 22050;
	Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
	int audio_channels = 2;
	int audio_buffers = 4096;

	if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers))
	{
		throw SDLException("Cannot execute Mix_OpenAudio." + std::string(Mix_GetError()));
	}

	// Load muted setting...
	fMuted = si::ConfigManager::get().isMuted();


}


void SDLAudioManager::startMusic(std::string location, RepeatStyle style)
{
	if(fMuted)
		return;
	Mix_PlayMusic(SDLResourceManager::get().getMusic(location).get(), style);


}

void SDLAudioManager::stopMusic()
{
	Mix_HaltMusic();
	Mix_FreeMusic(fMusic.get());

}
SDLAudioManager::~SDLAudioManager()
{
	Mix_CloseAudio();

}

void SDLAudioManager::playSound(std::string location, RepeatStyle style)
{
	if(fMuted)
		return;

	if(Mix_PlayChannel(-1, SDLResourceManager::get().getChunk(location).get(), style) == -1)
	{
		// If this doesn't work, too bad, not worth crashing over.
		return;
	}



}

void SDLAudioManager::stopSound(SoundID sound)
{
	assert(false);




}

void SDLAudioManager::beSilent()
{
	fMuted = true;
	si::ConfigManager::get().setMute(true);
}

void SDLAudioManager::beLoud()
{
	fMuted = false;
	si::ConfigManager::get().setMute(false);
}
}
